//
//  CardEditView.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/8/6.
//

import SwiftUI

struct CardEditView: View {
    @Environment(\.dismiss) private var dismiss
    @EnvironmentObject private var store: CardStore
    
    @State private var draft: [SignType]
    
    init() {
        _draft = State(initialValue: CardStore().orderedCards)
    }
    
    var body: some View {
        VStack {
            CustomNavigationBar(rightImageName: "card_save", title: "编辑数据卡片") {
                store.orderedCards = draft;
                AppState.shared.navigateBack()
            }
            
            List {
                Section(
                    footer: HStack() {
                        Spacer()
                        Text("长按右侧按钮可进行拖动，即可调整顺序")
                            .font(.system(size: 12, weight: .regular))
                            .foregroundStyle(Color(hex: "#999999"))
                        Spacer()
                    }
                        .listRowBackground(Color.clear)
                    .listRowSeparator(.hidden)
                    .padding(.top, 8)
                ) {
                    ForEach(draft, id: \.self) { card in
                        
                        CardItemView(signType: card)
                            .listRowInsets(EdgeInsets())
                            .listRowSeparator(.hidden)
                            .padding(.horizontal, 12)
                            .background(Color(hex: "#0A0A0A"))
                    }
                    .onMove { from, to in
                        draft.move(fromOffsets: from, toOffset: to)
                        print("draft =", draft)
                    }
                }

            }
            .listStyle(PlainListStyle())
            .listRowSpacing(12)
            .background(Color(hex: "#0A0A0A"))
            .environment(\.defaultMinListRowHeight, 0)
            
        }
        .navigationBarHidden(true)
        .background(Color(hex: "#0A0A0A"))
        .edgesIgnoringSafeArea(.all)
    }
}


struct CardItemView: View {
    
    let signType: SignType
    
    var body: some View {
        if signType == .header {
            HStack {
                Text(signType.name)
                    .font(.system(size: 16, weight: .medium))
                    .foregroundStyle(.white)
                Spacer()
            }
            .padding(.top, 12)
            .padding(.bottom, 4)
        } else {
            ZStack {
                Color(hex: "#1E1E1E")
                    .cornerRadius(16, corners: .allCorners)
                HStack {
                    Image(signType.icon)
                        .resizable()
                        .frame(width: 32, height: 32)
                    Text(signType.name)
                        .font(.system(size: 16, weight: .medium))
                        .foregroundStyle(.white)
                        .padding(.leading, 12)
                    Spacer()
                    Image("card_pan")
                        .resizable()
                        .frame(width: 32, height: 32)
                }
                .padding(.horizontal, 16)
                .padding(.vertical, 12)
            }
        }
    }
}

#Preview {
    CardEditView()
}
